package com.example.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.bbs.entity.Admin;
import com.example.bbs.entity.User;
import com.example.bbs.mapper.AdminMapper;
import com.example.bbs.mapper.UserMapper;
import com.example.bbs.service.UserLogin;
import com.example.bbs.util.ConstUtil;
import com.example.bbs.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2021/5/3 17:35
 */
@Service
public class UserLoginImpl implements UserLogin {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public String checkName() {
        return null;
    }

    @Override
    public String checkLoginInfo(HashMap<String, String> paramMap, HttpSession session) {
        String username = paramMap.get("username");
        String password = paramMap.get("password");
        //System.out.println(username + password);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", username).eq("password", MD5Util.MD5Encode(password, "utf-8"));
        List<User> users = userMapper.selectList(queryWrapper);
        if (users == null || users.size() == 0) {
            return "400";
        }
        session.setAttribute("user", users.get(0));
        return "200";
    }

    @Override
    public String checkRegisterInfo(HashMap<String, String> paramMap, HttpSession session) {
        String username = paramMap.get("username").trim();
        String password = paramMap.get("password").trim();

        List<User> users = userMapper.selectList(new QueryWrapper<User>().eq("name", username));
        if (users != null && users.size() != 0) {
            return "400";
        }
        User user = new User();
        user.setName(username);
        user.setPassword(MD5Util.MD5Encode(password, "utf-8"));
        user.setImg(ConstUtil.DEFAULT_USER_IMG);
        user.setLevel(1);
        user.setRegisterdate(LocalDate.now());
        int insert = userMapper.insert(user);
        if (insert > 0) {
            session.setAttribute("user", user);
            return "200";
        }
        return "500";
    }

    @Override
    public String checkAdminLoginInfo(HashMap<String, String> paramMap, HttpSession session) {
        String username = paramMap.get("username");
        String password = paramMap.get("password");
        System.out.println(username + password);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", username).eq("password", MD5Util.MD5Encode(password, "utf-8"));
        List<Admin> admins = adminMapper.selectList(queryWrapper);
        if (admins == null || admins.size() == 0) {
            return "400";
        }
        session.setAttribute("adminitor", admins.get(0));
        return "200";
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.MD5Encode("123456", "utf-8"));
    }

}
